﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Marmat.Foundation;

namespace Marmat.Template.WinForm
{
    /// <summary>
    /// 编辑窗体的默认实现。
    /// </summary>
    internal partial class ColumnConfigurationForm : Marmat.Forms.Form
    {
        private readonly Dictionary<ColumnView, Rectangle> dicRectangle = new();

        public ColumnConfigurationForm()
        {
            this.InitializeComponent();
        }

        private void listBox2_Paint(object sender, PaintEventArgs e)
        {
            if (sender is Marmat.Forms.Skin.ListBox listBox)
            {
                //e.Graphics.DrawRectangle(Pens.Gainsboro, new Rectangle(0, 0, listBox.ClientSize.Width - 1, listBox.Height - 1));
            }
        }

        //bool IColumnViewConfiguration.Configuration(ColumnViewCollection source, out IEnumerable<ColumnView> current)
        //{
        //    if (source is null)
        //        throw new ArgumentNullException(nameof(source));

        //    foreach (var column in source)
        //    {
        //        this.listBox1.Items.Add(column);
        //        if (column.Visible)
        //            this.listBox2.Items.Add(column);
        //    }
        //    var dialog = this.ShowDialog();
        //    return dialog != DialogResult.OK;
        //}

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Color backColor = Color.White;
            if (e.State.HasFlag(DrawItemState.HotLight))
                backColor = Color.FromArgb(250, 250, 250);

            using var brush = new SolidBrush(backColor);
            e.Graphics.FillRectangle(brush, e.Bounds);
            if (sender is Marmat.Forms.Skin.ListBox listBox && listBox.Items[e.Index] is ColumnView column)
            {
                TextRenderer.DrawText(e.Graphics, column.Caption ?? column.Name, e.Font, new Rectangle(e.Bounds.X + 12, e.Bounds.Top, e.Bounds.Width - 12, e.Bounds.Height), Color.Gray, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                if (column.Visible)
                {
                    Image img = Properties.Resources._checked;
                    int left = e.Bounds.Right - img.Width - 12;
                    int top = e.Bounds.Top + (e.Bounds.Height - img.Height) / 2;
                    e.Graphics.DrawImage(img, new Rectangle(left, top, img.Width, img.Height));
                }
            }
        }

        private void listBox2_DrawItem(object sender, DrawItemEventArgs e)
        {
            Color backColor = Color.White;
            if (e.State.HasFlag(DrawItemState.Selected))
                backColor = Color.FromArgb(240, 240, 240);
            else if (e.State.HasFlag(DrawItemState.HotLight))
                backColor = Color.FromArgb(250, 250, 250);

            using var brush = new SolidBrush(backColor);
            e.Graphics.FillRectangle(brush, e.Bounds);
            if (sender is Marmat.Forms.Skin.ListBox listBox && listBox.Items[e.Index] is ColumnView column)
            {
                TextRenderer.DrawText(e.Graphics, column.Caption ?? column.Name, e.Font, new Rectangle(e.Bounds.X + 12, e.Bounds.Top, e.Bounds.Width - 12, e.Bounds.Height), Color.Gray, TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
                if (column.Visible)
                {
                    Image img = Properties.Resources.delete_default;
                    int left = e.Bounds.Right - img.Width - 12;
                    int top = e.Bounds.Top + (e.Bounds.Height - img.Height) / 2;
                    var img_rect = new Rectangle(left, top, img.Width, img.Height);
                    dicRectangle[column] = img_rect;
                    e.Graphics.DrawImage(img, img_rect);
                }
            }
        }

        private void listBox1_ItemClick(object sender, Forms.Skin.ItemEventArgs e)
        {
            if (e.Item is ColumnView column)
            {
                column.Visible = !column.Visible;
                if (column.Visible)
                    this.listBox2.Items.Add(column);
                else if (this.listBox2.Items.Contains(column))
                    this.listBox2.Items.Remove(column);
                this.listBox1.Invalidate();
            }
        }

        private void listBox2_ItemClick(object sender, Forms.Skin.ItemEventArgs e)
        {
            var position = this.listBox2.PointToClient(Control.MousePosition);
            if (e.Item is ColumnView column && dicRectangle.ContainsKey(column) && dicRectangle[column].Contains(position))
            {
                column.Visible = false;
                this.listBox2.Items.Remove(column);
                this.listBox1.Invalidate();
            }
        }

        /// <summary>
        /// 上移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void skinButton3_Click(object sender, EventArgs e)
        {
            if (this.listBox2.SelectedIndex > 0)
            {
                var selected = this.listBox2.SelectedItem;
                this.listBox2.Items.Remove(selected);
                this.listBox2.Items.Insert(this.listBox2.SelectedIndex - 1, selected);
                this.listBox2.SelectedItem = selected;
            }
        }

        /// <summary>
        /// 下移
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void skinButton4_Click(object sender, EventArgs e)
        {
            if (this.listBox2.SelectedIndex >= 0 && this.listBox2.SelectedIndex < this.listBox2.Items.Count - 1)
            {
                var selected = this.listBox2.SelectedItem;
                this.listBox2.Items.Remove(selected);
                this.listBox2.Items.Insert(this.listBox2.SelectedIndex + 1, selected);
                this.listBox2.SelectedItem = selected;
            }
        }
    }
}
